﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace wnxd_feature
{
    public class feature
    {
        private static Database database;
        static feature()
        {
            database = DatabaseFactory.CreateDatabase();
        }
        public bool AddFeature(int CategoryId, String feature)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select count(*) from wnxd_feature where CategoryId={0}", CategoryId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            int count = (int)database.ExecuteScalar(cmd);
            sql.Clear();
            if (count != 0)
            {
                sql.AppendFormat("update wnxd_feature set Value='{0}' where CategoryId={1}", this.GetFeatureText(CategoryId) + "," + feature, CategoryId);
            }
            else
            {
                sql.AppendFormat("insert into wnxd_feature(CategoryId,Value) values({0},'{1}')", CategoryId, feature);
            }
            cmd = database.GetSqlStringCommand(sql.ToString());
            return database.ExecuteNonQuery(cmd) > 0;
        }
        public bool AddFeature(int CategoryId, String[] feature)
        {
            return this.AddFeature(CategoryId, string.Join(",", feature));
        }
        public bool DelFeature(int CategoryId, string feature)
        {
            string[] v = this.GetFeature(CategoryId);
            if (v.Length == 0)
            {
                return false;
            }
            int index = Array.IndexOf(v, feature);
            if (index == -1)
            {
                return false;
            }
            v[index] = "";
            return this.SetFeature(CategoryId, v);
        }
        public bool DelFeature(int CategoryId, int index)
        {
            string[] v = this.GetFeature(CategoryId);
            if (v.Length == 0)
            {
                return false;
            }
            index--;
            if (index < 0 && index >= v.Length)
            {
                return false;
            }
            v[index] = "";
            return this.SetFeature(CategoryId, v);
        }
        public bool SetFeature(int CategoryId, string feature)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select count(*) from wnxd_feature where CategoryId={0}", CategoryId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            int count = (int)database.ExecuteScalar(cmd);
            sql.Clear();
            if (count != 0)
            {
                sql.AppendFormat("update wnxd_feature set Value='{0}' where CategoryId={1}", feature, CategoryId);
            }
            else
            {
                sql.AppendFormat("insert into wnxd_feature(CategoryId,Value) values({0},'{1}')", CategoryId, feature);
            }
            cmd = database.GetSqlStringCommand(sql.ToString());
            return database.ExecuteNonQuery(cmd) > 0;
        }
        public bool SetFeature(int CategoryId, string[] feature)
        {
            return this.SetFeature(CategoryId, string.Join(",", feature));
        }
        public string[] GetFeature(int CategoryId)
        {
            string v = this.GetFeatureText(CategoryId);
            if (string.IsNullOrEmpty(v))
            {
                return new string[0];
            }
            return v.Split(',');
        }
        public string GetFeatureText(int CategoryId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select Value from wnxd_feature where CategoryId={0}", CategoryId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            object v = database.ExecuteScalar(cmd);
            if (v == null)
            {
                return "";
            }
            return v.ToString();
        }
        public int ProductId2CategoryId(int ProductId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select CategoryId from Hishop_Products where ProductId={0}", ProductId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            object obj = database.ExecuteScalar(cmd);
            if (obj == null)
            {
                return -1;
            }
            return (int)obj;
        }
        public bool ProductSet(int ProductId, int feature)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update Hishop_Products set feature=',{0},' where ProductId={1}", feature, ProductId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            return database.ExecuteNonQuery(cmd) > 0;
        }
        public bool ProductSet(int ProductId, string feature)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("update Hishop_Products set feature=',{0},' where ProductId={1}", feature, ProductId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            return database.ExecuteNonQuery(cmd) > 0;
        }
        public bool ProductSet(int ProductId, string[] feature)
        {
            return this.ProductSet(ProductId, string.Join(",", feature));
        }
        public bool ProductSet(int ProductId, int[] feature)
        {
            return this.ProductSet(ProductId, string.Join(",", feature.ToArray()));
        }
        public string[] ProductGet_S(int ProductId)
        {
            string v = this.ProductGetText(ProductId);
            if (string.IsNullOrEmpty(v))
            {
                return new string[0];
            }
            return v.Split(',');
        }
        public int[] ProductGet(int ProductId)
        {
            string[] v = this.ProductGet_S(ProductId);
            int[] list = new int[v.Length];
            for (int i = 0; i < v.Length; i++)
            {
                if (string.IsNullOrEmpty(v[i]))
                {
                    list[i] = 0;
                }
                else
                {
                    list[i] = int.Parse(v[i]);
                } 
            }
            return list;
        }
        public string ProductGetText(int ProductId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select feature from Hishop_Products where ProductId={0}", ProductId);
            DbCommand cmd = database.GetSqlStringCommand(sql.ToString());
            object v = database.ExecuteScalar(cmd);
            if (v == null)
            {
                return "";
            }
            return v.ToString();
        }
    }
}